<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="商品id" prop="productId">
            <el-input v-model="form.productId" placeholder="请输入商品id"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="数量" prop="num">
            <el-input v-model="form.num" placeholder="请输入数量"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="审核人id" prop="approveBy">
            <el-input v-model="form.approveBy" placeholder="请输入审核人id"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="审核时间" prop="approveTime">
            <el-input v-model="form.approveTime" placeholder="请输入审核时间"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" placeholder="类型">
              <el-option label="入库" :value="1"></el-option>
              <el-option label="领用" :value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" placeholder="选择订单状态">
              <el-option label="待审核" :value="1"></el-option>
              <el-option label="审核通过" :value="2"></el-option>
              <el-option label="审核拒绝" :value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>


        <el-col :span="12" class="mb20">
          <el-form-item label="拒绝原因" prop="refuseReason">
            <el-input v-model="form.refuseReason" placeholder="请输入拒绝原因"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注"/>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="BloodxApproveDialog">
import {useMessage} from "/@/hooks/message";
import {addObj, getObj, putObj} from '/@/api/blood/bloodxApprove'

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
  id: '',
  productId: '',
  num: '',
  approveBy: '',
  approveTime: '',
  type: '',
  status: '',
  refuseReason: '',
  remark: '',
});

// 定义校验规则
const dataRules = ref({})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取bloodxApprove信息
  if (id) {
    form.id = id
    getbloodxApproveData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getbloodxApproveData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
